﻿/***
 * 
 *    Title: "ZGUIFW"UI框架项目
 *           主题： 通用的配置管理器      
 *    Description: 
 *           功能： XXXX
 *    Date: 2018
 *    Version: 0.1版本
 */
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ZGUIFW{
	public class ConfigMgrByJson : IConfigMgr
    {
		private static Dictionary<string, string> _appSetting;
		public Dictionary<string, string> AppSetting { get { return _appSetting; } }

        public ConfigMgrByJson(string path)
		{
			_appSetting = new Dictionary<string, string>();
			InitAndAnalysisJson(path);
		}
		public int GetAppSettingMaxNumber()
		{
			if (_appSetting == null)
			    return 0;
			return _appSetting.Count;
		}

        /// <summary>
        /// Inits the and analysis json.
        /// </summary>
        /// <param name="path">Path.</param>
		private void InitAndAnalysisJson(string path)
		{
			TextAsset configInfo = null;
			keyValuesInfo keyValues = null;
			if (string.IsNullOrEmpty(path)) return;
			try
			{
				configInfo = Resources.Load<TextAsset>(path);
				keyValues = JsonUtility.FromJson<keyValuesInfo>(configInfo.text);
			}
			catch
			{
				throw new JsonAnlysisException(GetType() + "/InitAndAnalysisJson()...");
			}
			foreach (var v in keyValues.ConfigInfo)
			{
				_appSetting.Add(v.Key, v.Value);
			}
		}
	}
}

